import { getSetting, openSetting, chooseAddress,showModal ,showToast} from "../../utils/asyncWx.js"
/*1获取用户收货地址
 *   1.绑定点击事件
 *   2. 调用小程序的内置api  获取用户的收货地址 wx.chooseAddress
 * 2.获取用户对小程序授予的获取地址的权限状态 scope
 *   1,假设 用户点击获取收货地址的提示框 确定 scope =true
 *   
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  //点击收货地址触发点击事件
  async handleChooseAddress() {
    try {
      //1.获取权限状态
      const res1 = await getSetting()
      const scopeAddress = res1.authSetting["scope.address"]
      //判断权限状态
      if (scopeAddress == false) {
        //1.引导用户打开授权页面
        await openSetting()
      }
      // 3.调用获取收货地址
      const address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
      // console.log(res2);
      //  保存到本地缓存中
      wx.setStorageSync('address', address)
    } catch (error) {
      console.log(error);
    }
  },
  //商品选择 
  handeItemChange(e) {
    //获取修改商品的id
    const goods_id = e.currentTarget.dataset.id;
    console.log(goods_id);
    //获取购物车的商品数组
    let { cart } = this.data;
    //找到被修改的对象
    let index = cart.findIndex(v => v.goods_id == goods_id)
    //选中状态取反
    cart[index].checked = !cart[index].checked
    //把购物车数据和缓存数据都修改
    this.setCart(cart)
  },
  //设置购物车的状态同时 重新计算底部工具栏的数据 
  setCart(cart) {
    //计算全选
    let allChecked = true;
    //1总价格 总数量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    })
    //判断数组是否为空
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    })
    wx.setStorageSync('cart', cart)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取缓存的收货地址
    let address = wx.getStorageSync('address')
    //获取缓存的购物车数据
    let cart = wx.getStorageSync('cart') || []
    this.setCart(cart)
    this.setData({ address })
  },
  //商品反全选功能
  handleItemAllCheck() {
    //获取data的数据
    let { cart, allChecked } = this.data;
    //2.修改值
    allChecked = !allChecked
    //修改循环cart数组中商品的选中状态
    cart.forEach(v => v.checked = allChecked)
    //4.把修改的值填充回data或者缓存中
    this.setCart(cart)
  },
  //商品数量的编辑功能
  async handleItemNumEdit(e) {
    console.log(e);
    //获取传递的参数
    const { operation, id } = e.currentTarget.dataset;
    // console.log(operation ,id);
    //获取购物车数组
    let { cart } = this.data
    //找到需要修改商品的索引
    let index = cart.findIndex(v => v.goods_id == id)
    //判断用户是否执行删除
    if (cart[index].num == 1 && operation == -1) {
      const res = await showModal({ content: '你是否进行删除' })
      if (res.confirm) {
        cart.splice(index, 1)
        this.setCart(cart)
      } else if (res.cancel) {
        //进行修改状态
        cart[index].num += operation
        //设置回缓存data中
        this.setCart(cart)
      }
    }
  },
  //点击商品结算功能
  async handlePay(){
    console.log('111');
    const {address,totalNum} =this.data
    //1.判断收货地址
    if(!address.userName){
      await showToast({title:'你还木有选择收货地址'})
      return
    }
    //2.判断有没有选中商品
    if(totalNum ===0){
      await showToast({title:'你还木有选中商品'})
      return
    }
    //跳转支付页面
    wx.navigateTo({
      url: '/pages/pay/pay'
    });
      
  }
})